/**
 * Created by liubin on 2017/5/19.
 */


var DzpkTableRoomEndLayer = GameCCSBase.extend({
    mJuEndedEventdzpk : null,
    ctor: function () {
        this._super();
        this.loadCocostudio("res/ccs/DzpkTableRoomEnd.json");
        mo.gameHelper.fixIosText(this.ccsNode);

        mo.seekWidgetByName(this.ccsNode, "Button_ok").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                //如果不在圈子内 就申请加入该圈子
                // mo.dzpkTable.quitDzpkGame();
                if (mo.sceneHelper.currentLayerName == "login"){
                    //在登录界面就会带登录
                    mo.sceneHelper.intoLoginLayer();
                }else{
                    mo.sceneHelper.runSceneHall();
                }
                mo.sceneHelper.closeEndInfoLayer();
            }
        }, this);

        mo.seekWidgetByName(this.ccsNode, "Button_share").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                mo.gameHelper.onWxShareFriend(mo.gameHelper.projectName + "棋牌-牌局结算-德州扑克", "我完成了一个" + mo.gameHelper.projectName + "棋牌的牌局，这个是结算截图！");
            }
        }, this);

        this.loadUI();

        this.setVisible(false);

        mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_time"), mo.gameHelper.projectName + "卡五星");
    },

    onEnter : function(){
        this._super();

        mo.eventHelper.add("PushRYScore", this, function(PushRYScore){
            this.setHonorInfo(PushRYScore);
        });
    },

    onExit : function(){
        mo.eventHelper.remove(this);
        this._super();
    },

    loadUI: function () {

    },

    setRoomEndInfo : function(juEndedEventdzpk){
        this.mJuEndedEventdzpk = juEndedEventdzpk;

        //更新用户信息
        this.updatePlayerInfos();

        //时间
        var date = new Date(juEndedEventdzpk.EndTime * 1000);
        // var time = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " ";
        // time += (date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds());
        mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_time"), mo.gameHelper.getTimeYYMMDD(date));

        //房间号
        if (mo.gameHelper.developerVersion == "3.0"){
            mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_roomId"), "密码:" + mo.dzpkTable.mTableEnterPwd);
        }else if (mo.gameHelper.developerVersion == "2.0"){
            mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_roomId"), "房间号:" + mo.dzpkTableStatus.mTableStatus.TableID);
        }

        //局数
        var totalJu = mo.dzpkTable.mGameConfig.maxpan;
        if(juEndedEventdzpk.EndFlag == 0){
            var jushuStr = "局数:" + totalJu + "/" + totalJu;
            mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_juCount"), jushuStr);
        }else{
            var jushuStr = "局数:" + (parseInt(totalJu) - juEndedEventdzpk.LeftPlayTimes) + "/" + totalJu;
            mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_juCount"), jushuStr);
        }

        //缩放
        var Panel_center = mo.seekWidgetByName(this.ccsNode, "Panel_center");
        var contentSize = this.mJuEndedEventdzpk.Ju.length * 230;
        if (mo.gameSize.x - 50 < contentSize){
            var scale = (mo.gameSize.x - 50) / contentSize;
            Panel_center.setScale(scale);
        }else{
            Panel_center.setScale(1);
        }

        //礼券
        var Text_liquan = mo.seekWidgetByName(this.ccsNode, "Text_liquan");
        Text_liquan.setVisible(false);
        for (var i = 0; i < juEndedEventdzpk.Ju.length; i++){
            if (juEndedEventdzpk.Ju[i].Uid == mo.gameUserInfo.uid){
                if (juEndedEventdzpk.Ju[i].Coupon != null && juEndedEventdzpk.Ju[i].Coupon != 0){
                    mo.gameHelper.setTextString(Text_liquan, "礼券+" + juEndedEventdzpk.Ju[i].Coupon);
                    Text_liquan.setVisible(true);
                }
            }
        }

        //结束原因
        var ruleJson = JSON.parse(juEndedEventdzpk.CfgJson);
        var Text_exitInfo = mo.seekWidgetByName(this.ccsNode, "Text_exitInfo");
        if (juEndedEventdzpk.EndFlag == 0){
            mo.gameHelper.setTextString(Text_exitInfo, "自然结束");
        }else if (juEndedEventdzpk.EndFlag == 1){
            mo.gameHelper.setTextString(Text_exitInfo, "用户投票结束");
        }else if (juEndedEventdzpk.EndFlag == 2){
            mo.gameHelper.setTextString(Text_exitInfo, "生死战结束,生死战封顶:" + ruleJson.lod);
        }
    },

    updatePlayerInfos : function(){
        var playerLength = this.mJuEndedEventdzpk.Ju.length;
        var maxPlayer = null;
        var maxScore = -999;
        var startPx = -(230 * (playerLength - 1)) / 2;
        for (var i = 0; i < 5; i++){
            var player = mo.seekWidgetByName(this.ccsNode, "Image_player" + i);
            if (i < playerLength){
                player.setVisible(true);
                player.setPosition(cc.p(startPx + i * 230, 0));

                var juInfo = this.mJuEndedEventdzpk.Ju[i];

                //昵称
                mo.gameHelper.setTextString(mo.seekWidgetByName(player, "Text_nick"), mo.gameHelper.getUserNickFilter(juInfo.Nick));

                //头像
                mo.gameHelper.loadImageForUrl(mo.seekWidgetByName(player, "Image_head"), juInfo.FaceUrl);

                //uid
                mo.gameHelper.setTextString(mo.seekWidgetByName(player, "Text_id"), "ID:" + juInfo.Uid);

                //danju
                mo.gameHelper.setTextString(mo.seekWidgetByName(player, "Text_score0"), "" + juInfo.MaxScorePerPan);

                //zuida
                mo.gameHelper.setTextString(mo.seekWidgetByName(player, "Text_score1"), mo.dzpkCardHelper.getCardTypeCHS(juInfo.MaxCardType));

                //shengli
                var shengfuStr = "胜" + juInfo.WinTimes + "负" + juInfo.LostTimes;
                mo.gameHelper.setTextString(mo.seekWidgetByName(player, "Text_score2"), shengfuStr);

                //房主
                if (this.mJuEndedEventdzpk.CreateUserSeat == juInfo.Seat){
                    mo.seekWidgetByName(player, "Image_fangzhu").setVisible(true);
                }else{
                    mo.seekWidgetByName(player, "Image_fangzhu").setVisible(false);
                }

                //自己
                if (juInfo.Uid == mo.gameUserInfo.uid){
                    mo.seekWidgetByName(player, "Image_mySeat").setVisible(true);
                }else{
                    mo.seekWidgetByName(player, "Image_mySeat").setVisible(false);
                }

                //大赢家
                if (juInfo.Score > maxScore){
                    maxPlayer = player;
                    maxScore = juInfo.Score;
                }

                mo.seekWidgetByName(player, "Image_dayingjia").setVisible(false);

                //分数
                var Fnt_score = mo.seekWidgetByName(player, "Fnt_score");
                var scoreStr = juInfo.Score;
                if (scoreStr >= 0){
                    Fnt_score.setFntFile("res/ccs/dzpk/end/bigWinFnt.fnt");
                    scoreStr = "+" + scoreStr;
                }else{
                    Fnt_score.setFntFile("res/ccs/dzpk/end/bigLoseFnt.fnt");
                    scoreStr = "" + scoreStr;
                }
                Fnt_score.setString(scoreStr);

            }else{
                player.setVisible(false);
            }
        }


        //大赢家
        if (maxPlayer != null){
            mo.seekWidgetByName(maxPlayer, "Image_dayingjia").setVisible(true);
        }
    }
});